import { createApp } from 'vue'
import App from './App.vue'
import "@/assets/less/index.less"
import router from "./router/index.js";
import ElementPlus from "element-plus";
import { createPinia } from "pinia";
import "@/api/mock.js";
import api from "@/api/api.js";
import {useAllDataStore} from "@/stores";
import "element-plus/dist/index.css";
import * as ElementPlusIconsVue from "@element-plus/icons-vue";

function isRoute(to){
    return router.getRoutes().filter(item => item.path === to.path).length>0;
}
router.beforeEach((to,from)=>{
    if(to.path!== '/login' &&!store.state.token){
        return {name:"login"};
    } 
    if(!isRoute(to)){
        return {name:"main"};
    }
});

const app = createApp(App);

//自动引入图标
Object.keys(ElementPlusIconsVue).forEach((key) => {
    app.component(key, ElementPlusIconsVue[key]);
});

const pinia = createPinia();
app.use(pinia);

// 在这里创建存储实例
const store = useAllDataStore();
store.addMenu(router, "refresh");

app.config.globalProperties.$api = api;
app.use(router).use(ElementPlus);
app.mount('#app');
